package com.west_traveler.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.west_traveler.application.BaseApplication;
import com.west_traveler.entity.tab.Tab;
import com.west_traveler.entity.tab.TabName;
import com.west_traveler.inteface.HttpService;
import com.west_traveler.viewpager_adapter.FindPageMainViewPagerAdapter;
import com.west_traveler.ximalayafm.MainActivity;
import com.west_traveler.ximalayafm.R;
import com.west_traveler.ximalayafm.SearchActivity;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Description: 发现页Fragment
 * Date:2016年12月02日15:10
 *
 * @author: 刘悦.
 */

public class FindFragment extends Fragment implements Callback<Tab> {


    private Context mContext;
    private MainActivity mActivity;
    private Toolbar toolbar;
    private ViewPager viewPager;
    private TabLayout tabLayout;

    public static FindFragment getInstance() {
        return new FindFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_find, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        mContext = getContext();
        mActivity = (MainActivity) getActivity();
        setHasOptionsMenu(true);

        initView(view);
        toolbar.setTitle("");
        mActivity.setSupportActionBar(toolbar);

        BaseApplication application = (BaseApplication) mActivity.getApplication();
        HttpService service = application.getService();
        service.getTabs("android")
                .enqueue(this);

    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.option_menu_search, menu);
        MenuItem item = menu.findItem(R.id.find_page_search_id);
        ImageView search = (ImageView) item.getActionView();
        search.setImageResource(R.drawable.search_flag_image);

        aboutSearch(search);

    }

    private void aboutSearch(ImageView search) {

//        点击打开搜索界面
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, SearchActivity.class);
                startActivity(intent);
                mActivity.overridePendingTransition(R.anim.push_left_in,android.R.anim.fade_out);
            }
        });

    }

    private void initView(View view) {
        toolbar = (Toolbar) view.findViewById(R.id.tb_find_title_bar_id);
        viewPager = (ViewPager) view.findViewById(R.id.vp_find_main_id);
        tabLayout = (TabLayout) view.findViewById(R.id.tl_tab_id);
    }

    @Override
    public void onResponse(Call<Tab> call, Response<Tab> response) {
        List<TabName> tabNames = response.body().getTabs().getList();
        FindPageMainViewPagerAdapter adapter = new FindPageMainViewPagerAdapter(getChildFragmentManager(), tabNames);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
    }

    @Override
    public void onFailure(Call<Tab> call, Throwable t) {
        Toast.makeText(mContext, t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
